<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>登录</title>
    <!-- 地址栏显示 -->
    <link rel="Shortcut Icon" href="/images/hospital.ico" type="image/x-icon"/>
    <!-- 收藏栏也显示 -->
    <link rel="Bookmark" href="/images/hospital.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>

<!-- 你的 HTML 代码 -->
<div class="layui-container">
    <div class="layui-row">
        <fieldset class="layui-elem-field layui-col-md4 layui-col-md-offset4">
            <legend>登录</legend>
            <div class="layui-field-box">
                <form class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">邮箱</label>
                        <div class="layui-input-block">
                            <input type="text" name="email" required="" lay-verify="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input" value="1208585122@qq.com">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="password" name="password" required="" lay-verify="password" placeholder="请输入密码" autocomplete="off" class="layui-input" value="123">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="login">立即登录</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
    </div>
</div>
<script src="/layui/layui.js"></script>
<script>
    layui.use(['jquery','layer', 'form'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var $=layui.jquery;
        form.verify({
            email:function (value,item){
                if(/^\d+\d+\d$/.test(value)){
                    return '用户名不能全为数字';
                }
            },
            password:function (value,item){
                if(!/^.{3,12}$/img.test(value)){
                    return"密码必须位3~12位"
                }
            },
        });
       form.on('submit(login)', function(data){
           var user = {
               email:$('input[name="email"]').val(),
               password:$('input[name="password"]').val()
           };
           $.ajax({
               url:'/dologin',
               type:'post',
               contentType: 'application/json',
               dataType:'json',
               data: JSON.stringify(user),
               beforeSend:function (){
                   //弹出的lodinng层
                   layer.load(2,{
                       shade:[0.5,"#333"]
                   });
               },
               success: function (data) {
                    layer.msg("网络请求成功")
                    if (data.status==1){
                        layer.msg("登陆成功")
                        setTimeout(function (){
                            location.href="/main";
                        },2000)
                    }else {
                        layer.msg(data.message)
                    }
               },
               error: function () {
                   layer.msg("网络请求失败")
               },
               complete:function () {
                   layer.closeAll("loading")
               }
           });
           return false;
       });
    });
</script>
</body>
</html>